﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Table;
using System.Configuration;
using BreadWebAPI.Models;

namespace BreadWebAPI.Controllers
{
    public class LogController : ApiController
    {
        CloudStorageAccount storageAccount;
        CloudTable table;

        public LogController()
        {
            // need to make this code execute only once
            storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);

            // Create the table client.
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

            // Create the CloudTable object that represents the "Logs" table.
            table = tableClient.GetTableReference("Logs");

        }

        public int Get([FromUri]string BreadDate)
        {
            return 0;
        }

        // POST api/log
        public void Post([FromBody]LogEntity value)
        {
            try
            {
                // Create the TableOperation that inserts the comment entity.
                TableOperation insertOperation = TableOperation.InsertOrReplace(value);

                // Execute the insert operation.
                TableResult tr = table.Execute(insertOperation);
            }
            catch (Exception ex)
            {
                return;
            }
        }

        public void Put(int id, [FromBody]string value)
        {
        }

        public void Delete(int id)
        {
        }
    }
}
